using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections.Generic;

namespace My.IdlConverter.Core.Filters
{
    using Parsers;

    public class ClassModifier
    {
        public static IList<string> ExcludeClasses = new List<string>();

        static ClassModifier()
        {
            ExcludeClasses.Add("Cv_DOMInnerInit");
            ExcludeClasses.Add("Cv_DOMCustomedItemAgent");
            ExcludeClasses.Add("Cv_DOMCollection");
            ExcludeClasses.Add("Cv_DOMUndoRedoManager");
            ExcludeClasses.Add("Cv_DOMSelectionObserver");
            ExcludeClasses.Add("Cv_DOMComposite");
            ExcludeClasses.Add("Cv_DOMTreeNode");
        }

        public static bool IsDOMClassName(string className)
        {
            if (Util.ArrayHelper.IndexOf<string>(ExcludeClasses,className.Trim()) != -1)
                return false;

            return className.Trim().StartsWith("Cv_DOM");
        }

        public static string GetTitle(Class source)
        {
            Debug.Assert(source != null);
            return GetTitle(source.Name);
        }

        public static string GetTitle(string className)
        {
            int pos = className.IndexOf("Cv_DOM");
            string target = className;
            if (pos != -1)
            {
                target = className.Replace("Cv_DOM", "");
                target = target.Trim();
            }
            else
            {
                Debug.Assert(false);
            }

            return target;
        }

        public static string GetName(Class source)
        {
            return ("C"+GetTitle(source));
        }

        public static string GetName(string className)
        {
            return ("C" + GetTitle(className));
        }

        public static bool ExtraceInterfaceName(string entry, out string result)
        {
            result = "";

            if (!Regex.IsMatch(entry, ClassParser.IV_DOM_REGEX))
                return false;
            result = Regex.Match(entry, ClassParser.IV_DOM_REGEX).Value;

            result = result.Replace('(', ' ');
            result = result.Replace(')', ' ');
            result = result.Trim();

            return true;
        }

        public static bool ExtraceClassName(string entry, out string result)
        {
            result = "";

            if (!Regex.IsMatch(entry, ClassParser.CV_DOM_REGEX))
                return false;
            result = Regex.Match(entry, ClassParser.CV_DOM_REGEX).Value;

            result = result.Replace('(', ' ');
            result = result.Replace(')', ' ');
            result = result.Trim();

            return true;
        }
    }
}
